package cn.hncu.booksManagementSystem.book.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.book.business.ebi.BookEbi;
import cn.hncu.booksManagementSystem.book.business.factory.BookEbiFactory;
import cn.hncu.booksManagementSystem.book.vo.BookModel;

public class UpdatePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField tfdId;
	private JTextField tfdName;
	
	private JFrame mainFrame;
	private BookModel oldBook;
	
	//注入ebi
	private BookEbi ebi = BookEbiFactory.getInstanceOfBookEbi();
	private JTextField tfdIn;
	private JTextField tfdOut;
	/**
	 * Create the panel.
	 */
	public UpdatePanel(JFrame mainFrame,BookModel oldBook) {
		this.mainFrame = mainFrame;
		this.oldBook = oldBook;
		initContentPane();
	}
	
	public void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("修改图书");
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setBounds(285, 33, 210, 49);
		add(label);
		
		JLabel lblid = new JLabel("编号：");
		lblid.setFont(new Font("微软雅黑", Font.BOLD, 24));
		lblid.setHorizontalAlignment(SwingConstants.CENTER);
		lblid.setBounds(82, 141, 121, 34);
		add(lblid);
		
		tfdId = new JTextField(oldBook.getId());
		tfdId.setEnabled(false);
		tfdId.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdId.setBounds(210, 142, 171, 33);
		add(tfdId);
		tfdId.setColumns(10);
		
		JLabel label_1 = new JLabel("名称：");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_1.setBounds(432, 141, 105, 34);
		add(label_1);
		
		tfdName = new JTextField(oldBook.getName());
		tfdName.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdName.setColumns(10);
		tfdName.setBounds(547, 141, 171, 33);
		add(tfdName);
		
		JButton btnUpdate = new JButton("修改");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//1收集参数 id,name,inPrice
				String id = tfdId.getText();
				String name = tfdName.getText();
				String strInPrice = tfdIn.getText();
				String strOutPrice = tfdOut.getText();
				double inPrice = 0.0;
				double outPrice = 0.0;
				if (name==null || name.trim().length()==0) {
					JOptionPane.showMessageDialog(null, "图书名不能为空！");
					return;
				}
				
				if (strInPrice==null||strInPrice.trim().length()==0) {
					JOptionPane.showMessageDialog(null, "图书进价不能为空！");
					return;
				}
				
				if (strOutPrice==null || strOutPrice.trim().length()==0) {
					JOptionPane.showMessageDialog(null, "图书售价不能为空！");
					return;
				}
				try {
					inPrice = Double.parseDouble(strInPrice.trim());
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null, "图书进价格式错误，只能是浮点型");
				}
				try {
					outPrice = Double.parseDouble(strOutPrice.trim());
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null, "图书售价格式错误，只能是浮点型");
				}
				
				
				//2组织参数
				BookModel newBook = new BookModel();
				newBook.setId(id);
				newBook.setName(name);
				newBook.setInPrice(inPrice);
				newBook.setOutPrice(outPrice);
				//3 调用逻辑层
				boolean boo = ebi.update(newBook);
				//4 导向结果界面
				if (boo) {
					mainFrame.setContentPane(new ListPanel(mainFrame));
					mainFrame.validate();
				}else {
					JOptionPane.showMessageDialog(null, "系统繁忙！");
				}
			}
		});
		btnUpdate.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnUpdate.setBounds(209, 333, 131, 49);
		add(btnUpdate);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mainFrame.setContentPane(new ListPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnBack.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnBack.setBounds(432, 333, 131, 49);
		add(btnBack);
		
		tfdIn = new JTextField(oldBook.getInPrice()+"");
		tfdIn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdIn.setColumns(10);
		tfdIn.setBounds(210, 248, 171, 33);
		add(tfdIn);
		
		JLabel label_2 = new JLabel("进价：");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_2.setBounds(95, 248, 105, 34);
		add(label_2);
		
		tfdOut = new JTextField(oldBook.getOutPrice()+"");
		tfdOut.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdOut.setColumns(10);
		tfdOut.setBounds(547, 248, 171, 33);
		add(tfdOut);
		
		JLabel label_3 = new JLabel("售价：");
		label_3.setHorizontalAlignment(SwingConstants.CENTER);
		label_3.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_3.setBounds(432, 248, 105, 34);
		add(label_3);
	}
}
